package com.iisigroup.sonar.httpclient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class JsonClient01 extends BaseCommonHttpMethod {
	public static void main(String[] args) {
		new JsonClient01().method03();
	}

	private static String prefixUrl = "http://192.168.7.143:9000";

	public void method01() {
		final CloseableHttpClient httpclient = getHttpClient(null, 0);

		final HttpPost httpPost = new HttpPost(prefixUrl
				+ "/api/issues/search");
		final List<NameValuePair> nvps = new ArrayList<NameValuePair>();

		nvps.add(new BasicNameValuePair("ID", "2240"));
		CloseableHttpResponse response = null;

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF8"));
			response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine());
			HttpEntity entity2 = response.getEntity();
			String content = EntityUtils.toString(entity2);
			
			// do something useful with the response body
			// and ensure it is fully consumed
			
			EntityUtils.consume(entity2);
		} catch (final Exception e) {
			System.out.println("ERR:" + e.toString());
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					System.out.println("ERR:" + e.toString());
				}
			}
		}

	}

	protected String commonRetrieveData(String param) {
		String result = null;
		final CloseableHttpClient httpclient = getHttpClient(null, 0);
		CloseableHttpResponse response = null;

		try {
			final HttpGet httpPost = new HttpGet(prefixUrl

			/****
			 * reference
			 * http://docs.codehaus.org/pages/viewpage.action?pageId=229743280
			 * 
			 * *****/

			+ param);
			response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine());
			HttpEntity entity2 = response.getEntity();
			result = EntityUtils.toString(entity2);
			EntityUtils.consume(entity2);
		} catch (final Exception e) {
			System.out.println("ERR:" + e.toString());
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					System.out.println("ERR:" + e.toString());
				}
			}
		}
		try {
			httpclient.close();
		} catch (IOException e) {
			System.out.println("ERR:" + e.toString());
		}
		return result;
	}
	public void method03() {
		String param ="";
		param ="/api/projects/index";//可以取得全部的project Id
		param ="/api/issues/search";
		param = "/api/issues/search?componentRoots=peijun.jian:project";//指定projectId為peijun.jian:project時取得issue摘要資訊
//		param ="/api/metrics";//取得屬性表
//		param ="/api/metrics?key=ncloc_language_distribution";//取得屬性表 (不常用)
//		param ="/api/resources?resource=peijun.jian:project&metrics=ncloc,coverage";//Get lines of code and code coverage measures for project
//		param ="/api/resources?resource=peijun.jian:project&depth=1&metrics=ncloc";//(可以得到Lines Of Code)
		
		String iniData  = commonRetrieveData(param);
		System.out.println(iniData);
	}
	

}
